home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / ax25file.c < prev    next >
C/C++ Source or Header  |  1992-01-13  |  4KB  |  166 lines

  1. /* @(#) $Header: ax25file.c,v 1.7 92/01/12 18:39:54 deyke Exp $ */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #include "global.h"
  7. #include "timer.h"
  8. #include "iface.h"
  9. #include "ax25.h"
  10.  
  11. #define AXROUTE_FILE_VERSION    1
  12. #define AXROUTE_HOLDTIME        (0x7fffffff / 1000)
  13. #define AXROUTE_SAVETIME        (60L*10)
  14.  
  15. struct axroute_saverecord_0 {
  16.   char call[AXALEN];
  17.   char pad1;
  18.   char digi[AXALEN];
  19.   char pad2;
  20.   short dev;
  21.   long time;
  22. };
  23.  
  24. struct axroute_saverecord_1 {
  25.   char call[AXALEN];
  26.   char digi[AXALEN];
  27.   long time;
  28. /*char ifname[]; */
  29. };
  30.  
  31. static char axroute_filename[] = "/tcp/axroute_data";
  32. static char axroute_tmpfilename[] = "/tcp/axroute_tmp";
  33.  
  34. static int valid_call __ARGS((char *call));
  35.  
  36. /*---------------------------------------------------------------------------*/
  37.  
  38. void axroute_savefile()
  39. {
  40.  
  41.   FILE * fp;
  42.   int i;
  43.   static long nextsavetime;
  44.   struct ax_route *rp, *lp;
  45.   struct axroute_saverecord_1 buf;
  46.  
  47.   if (!nextsavetime) nextsavetime = secclock() + AXROUTE_SAVETIME;
  48.   if (Debug || nextsavetime > secclock()) return;
  49.   nextsavetime = secclock() + AXROUTE_SAVETIME;
  50.   if (!(fp = fopen(axroute_tmpfilename, "w"))) return;
  51.   putc(AXROUTE_FILE_VERSION, fp);
  52.   for (i = 0; i < AXROUTESIZE; i++)
  53.     for (lp = 0, rp = Ax_routes[i]; rp; )
  54.       if (rp->perm || rp->time + AXROUTE_HOLDTIME >= secclock()) {
  55.     addrcp(buf.call, rp->call);
  56.     if (rp->digi)
  57.       addrcp(buf.digi, rp->digi->call);
  58.     else
  59.       *buf.digi = '\0';
  60.     buf.time = rp->time;
  61.     fwrite((char *) & buf, sizeof(buf), 1, fp);
  62.     if (rp->ifp)
  63.       fwrite(rp->ifp->name, strlen(rp->ifp->name) + 1, 1, fp);
  64.     else
  65.       putc(0, fp);
  66.     lp = rp;
  67.     rp = rp->next;
  68.       } else if (lp) {
  69.     lp->next = rp->next;
  70.     free(rp);
  71.     rp = lp->next;
  72.       } else {
  73.     Ax_routes[i] = rp->next;
  74.     free(rp);
  75.     rp = Ax_routes[i];
  76.       }
  77.   fclose(fp);
  78.   rename(axroute_tmpfilename, axroute_filename);
  79. }
  80.  
  81. /*---------------------------------------------------------------------------*/
  82.  
  83. static int valid_call(call)
  84. char *call;
  85. {
  86.   char (*mpp)[AXALEN];
  87.  
  88.   if (!*call || ismyax25addr(call)) return 0;
  89.   for (mpp = Ax25multi; (*mpp)[0]; mpp++)
  90.     if (addreq(call, *mpp)) return 0;
  91.   return 1;
  92. }
  93.  
  94. /*---------------------------------------------------------------------------*/
  95.  
  96. void axroute_loadfile()
  97. {
  98.  
  99.   FILE * fp;
  100.   int version;
  101.   static int done;
  102.  
  103.   if (done) return;
  104.   done = 1;
  105.   if (Debug || !(fp = fopen(axroute_filename, "r"))) return;
  106.  
  107.   switch (version = getc(fp)) {
  108.  
  109.   default:
  110.     {
  111.  
  112.       struct ax_route *rp;
  113.       struct axroute_saverecord_0 buf;
  114.       struct iface *ifp, *ifptable[128];
  115.  
  116.       ungetc(version, fp);
  117.       memset(ifptable, 0, sizeof(ifptable));
  118.       for (ifp = Ifaces; ifp; ifp = ifp->next)
  119.     if (ifp->output == ax_output) ifptable[ifp->dev] = ifp;
  120.       while (fread((char *) & buf, sizeof(buf), 1, fp)) {
  121.     if (buf.time + AXROUTE_HOLDTIME < secclock()) continue;
  122.     if (!valid_call(buf.call)) continue;
  123.     rp = ax_routeptr(buf.call, 1);
  124.     if (valid_call(buf.digi)) rp->digi = ax_routeptr(buf.digi, 1);
  125.     if (buf.dev >= 0 && buf.dev < 128) rp->ifp = ifptable[buf.dev];
  126.     rp->time = buf.time;
  127.       }
  128.     }
  129.  
  130.   case 1:
  131.     {
  132.  
  133.       char *cp;
  134.       char ifname[1024];
  135.       int c;
  136.       struct ax_route *rp;
  137.       struct axroute_saverecord_1 buf;
  138.       struct iface *ifp;
  139.  
  140.       while (fread((char *) & buf, sizeof(buf), 1, fp)) {
  141.     cp = ifname;
  142.     do {
  143.       if ((c = getc(fp)) == EOF) {
  144.         fclose(fp);
  145.         return;
  146.       }
  147.     } while (*cp++ = c);
  148.     if (*ifname)
  149.       for (ifp = Ifaces; ifp && strcmp(ifp->name, ifname); ifp = ifp->next) ;
  150.     else
  151.       ifp = 0;
  152.     if (buf.time + AXROUTE_HOLDTIME < secclock()) continue;
  153.     if (!valid_call(buf.call)) continue;
  154.     rp = ax_routeptr(buf.call, 1);
  155.     if (valid_call(buf.digi)) rp->digi = ax_routeptr(buf.digi, 1);
  156.     rp->ifp = ifp;
  157.     rp->time = buf.time;
  158.       }
  159.     }
  160.  
  161.   }
  162.  
  163.   fclose(fp);
  164. }
  165.  
  166.